package com.example.mumu.passwordbook;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.andrognito.patternlockview.PatternLockView;
import com.andrognito.patternlockview.listener.PatternLockViewListener;
import com.andrognito.patternlockview.utils.PatternLockUtils;
import java.util.List;

import static com.example.mumu.passwordbook.common.Constants.UpdatePatternPasswordStatus.*;

/**
 * Created by lupan 2018/6/21
 */
public class PatternLockUpdateActivity extends AppCompatActivity {

    private PatternLockView mPatternLockView;
    private int status;
    private SharedPreferences sp ;
    private TextView tvHint;
    private String tempPassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pattern_lock);

        sp = PreferenceManager.getDefaultSharedPreferences(PatternLockUpdateActivity.this);

        setupActionBar();
        setupPatternLock();

    }


    private void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    private void setupPatternLock(){

        status = INPUT_OLD_PASSWORD;

        tvHint = findViewById(R.id.tv_hint);
        tvHint.setText(R.string.input_old_pattern_password);

        mPatternLockView = findViewById(R.id.pattern_lock_view);
        PatternLockViewListener mPatternLockViewListener = new PatternLockViewListener() {

            @Override
            public void onStarted() {
                Log.d(getClass().getName(), "Pattern drawing started");
            }

            @Override
            public void onProgress(List<PatternLockView.Dot> progressPattern) {
                Log.d(getClass().getName(), "Pattern progress: " +
                        PatternLockUtils.patternToString(mPatternLockView, progressPattern));
            }

            @Override
            public void onComplete(List<PatternLockView.Dot> pattern) {
                String inputPassword = PatternLockUtils.patternToString(mPatternLockView, pattern);
                switch(status){
                    case INPUT_OLD_PASSWORD:
                        String oldPassword = sp.getString("pattern_password",null);
                        if(oldPassword!=null && oldPassword.equals(inputPassword)){
                            status=INPUT_NEW_PASSWORD;
                            tvHint.setText(R.string.input_new_pattern_password);
                        }else{
                            tvHint.setText(R.string.input_old_pattern_password_wrong);
                            tvHint.setTextColor(getResources().getColor(R.color.pomegranate));
                        }
                        break;
                    case INPUT_NEW_PASSWORD:
                        if(inputPassword.length()>=5){
                            tempPassword = inputPassword;
                            status=REPEAT_NEW_PASSWORD;
                            tvHint.setText(R.string.input_repeat_new_pattern_password);
                        }else{
                            tvHint.setText(R.string.input_new_pattern_password_wrong);
                            tvHint.setTextColor(getResources().getColor(R.color.pomegranate));
                        }
                        break;
                    case REPEAT_NEW_PASSWORD:
                        if(inputPassword.equals(tempPassword)){
                            tempPassword = inputPassword;
                            //保存到sp
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("pattern_password",inputPassword);
                            editor.apply();

                            Toast toast = Toast.makeText(PatternLockUpdateActivity.this, getResources().getString(R.string.update_pattern_password_success), Toast.LENGTH_SHORT);
                            toast.show();

                            PatternLockUpdateActivity.this.finish();
                        }else{
                            tvHint.setText(R.string.input_repeat_new_pattern_password_wrong);
                            tvHint.setTextColor(getResources().getColor(R.color.pomegranate));
                        }
                        break;
                }
                mPatternLockView.clearPattern();
            }

            @Override
            public void onCleared() {
                Log.d(getClass().getName(), "Pattern has been cleared");
            }
        };

        mPatternLockView.addPatternLockListener(mPatternLockViewListener);
    }
}
